"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 工具gs_basebackup
Case Name   : gs_basebackup -D参数是否存在缓冲区溢出问题
Create At   : 2022/12/15 20:05
Owner       : x00454388
Description :
    1. 执行gs_basebackup -D {datadir}  -p {port}
Expect      :
    1. gs_basebackup -D参数不存在缓冲区溢出问题
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.security_config.security_config import security_config


class TestTools(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.case_num = os.path.basename(__file__).split(".")[0]
        self.logger.info(f'------case {self.case_num} start------')
        self.userNode = Node(security_config.get("node_conf_name") or 'default')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        # 判断工具是否已经安装
        is_exist_command = self.userNode.sh(
            f"source {self.DB_ENV_PATH};gs_basebackup -V").result()
        self.assertIn(
            member='compiled at',
            container=is_exist_command,
            msg=f"command error: {is_exist_command}")
        # 用例参数
        self.path = "A" * 4096
        self.port = self.userNode.db_port

    def test_security(self):
        text1 = '--------step1: 执行 gs_basebackup ;expect: 无core产生--------'
        self.logger.info(text1)
        excute_cmd1 = f'''source {self.DB_ENV_PATH};
                            gs_basebackup -D {self.path} -p {self.port}'''
        msg1 = self.userNode.sh(excute_cmd1).result()
        self.logger.info(msg1)

        self.assertNotIn(
            'Segmentation fault (core dumped)', msg1,
            f"执行失败 {text1}, command: {excute_cmd1},error_msg:{msg1}")

    def tearDown(self):
        self.userNode.sh(f"rm -rf {self.path}")
        self.logger.info(f'------case {self.case_num} end-----')
